package com.jinduplan.planmanager.ui.activity.clockin

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import androidx.recyclerview.widget.LinearLayoutManager
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener
import com.baidu.mapapi.search.sug.SuggestionResult
import com.baidu.mapapi.search.sug.SuggestionSearch
import com.baidu.mapapi.search.sug.SuggestionSearchOption
import com.cxz.kotlin.baselibs.adapter.CustomRecyclerAdapter
import com.cxz.kotlin.baselibs.adapter.ViewHolder
//import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener
//import com.baidu.mapapi.search.sug.SuggestionResult
//import com.baidu.mapapi.search.sug.SuggestionSearch
//import com.baidu.mapapi.search.sug.SuggestionSearchOption
import com.cxz.kotlin.baselibs.base.BaseMvpTitleActivity
import com.cxz.kotlin.baselibs.utils.GsonHelper
import com.cxz.kotlin.baselibs.utils.LogUtil
import com.jinduplan.planmanager.R
import com.jinduplan.planmanager.bean.BuildingNameBean
import com.jinduplan.planmanager.bean.ProjectUploadDetailBean
import com.jinduplan.planmanager.bean.UploadProjectLocation
import com.jinduplan.planmanager.constant.Constant
import com.jinduplan.planmanager.mvp.contract.ClockInContract
import com.jinduplan.planmanager.mvp.presenter.ClockInPresenter
import com.jinduplan.planmanager.utils.ResUtil
import kotlinx.android.synthetic.main.activity_clock_in_search.*


class ClockInSearchActivity : BaseMvpTitleActivity<ClockInContract.View, ClockInContract.Presenter>(),
    ClockInContract.View {

    val datas = ArrayList<SuggestionResult.SuggestionInfo>()
    val listener:OnGetSuggestionResultListener by lazy {
        object: OnGetSuggestionResultListener {
            override fun onGetSuggestionResult(list: SuggestionResult?) {
                LogUtil.d("onGetSuggestionResult!!!")
                if (null != list) {
                    datas.clear()
                    list.allSuggestions?.let { suggestions->

                        datas.addAll(suggestions)
                        rcyAddr.adapter?.notifyDataSetChanged()
                        suggestions.forEach {
                            LogUtil.d("onGetSuggestionResult it.address->${it.address},${GsonHelper.toJson(it)}")
                        }

                    }
                }
            }

        }
    }

    override fun attachChildLayoutRes(): Int {
        return R.layout.activity_clock_in_search
    }

    override fun createPresenter(): ClockInContract.Presenter {
        return ClockInPresenter()
    }

    override fun start() {
        setBaseTitle(ResUtil.getS(R.string.search))
        val city = intent.extras?.getString(Constant.CITY)
        ivDel.setOnClickListener {
            etSearch.setText("")
        }

        val mSuggestionSearch = SuggestionSearch.newInstance()
        etSearch.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                if (!etSearch.text.toString().isEmpty()) {
                    mSuggestionSearch.requestSuggestion(
                        SuggestionSearchOption().keyword(s.toString()).city(city)
                    )
                }
            }
        })

        mSuggestionSearch.setOnGetSuggestionResultListener(listener)
        rcyAddr.adapter = object : CustomRecyclerAdapter<SuggestionResult.SuggestionInfo>(
            this, R.layout.item_addr_search, datas
        ) {
            override fun convert(holder: ViewHolder, t: SuggestionResult.SuggestionInfo, position: Int) {
                holder.setText(R.id.tvAddr, t.address+t.key)
                holder.itemView.setOnClickListener {
                    val intent = Intent()
                    intent.putExtra(Constant.LATITUDE,t.pt.latitude)
                    intent.putExtra(Constant.LONGITUDE,t.pt.longitude)
                    intent.putExtra(Constant.POINAME,t.key)
                    intent.putExtra(Constant.ADDR,t.address)
                    setResult(88,intent)
                    finish()
                }
            }

        }
        rcyAddr.layoutManager = LinearLayoutManager(this)

    }

    override fun clockInSuccess() {

    }

    override fun uploadPicSuccess() {

    }

    override fun uploadPicSuccess(picUrl: String) {

    }

    override fun postProjectLocationSuccess(uploadProjectLocation: UploadProjectLocation) {

    }

    override fun setProjectUploadDetailListData(projectData: BuildingNameBean) {

    }

}